package com.digitalchina.lxnsy.activity.old;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.TIME;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.widget.EditTextWithClear;
import com.digitalchina.lxnsy.xml.UserXML;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：白彦东
 * 创建时间：2017/3/1 9:50
 * 修改备注：
 * ================================================
 */

public class MobilePhoneIdentificationStepOneActivity extends BaseActivity {

    @Bind(R.id.identification_tv_mobile)
    TextView identificationTvMobile;
    @Bind(R.id.identification_et_auth_code)
    EditTextWithClear identificationEtAuthCode;
    @Bind(R.id.identification_bt_auth_code)
    Button identificationBtAuthCode;
    @Bind(R.id.identification_bt_next)
    Button identificationBtNext;

    private String mStrMobile;
    private String mStrAuthCode;

    @Override
    public void setLayout() {
        setTitle("修改手机号");
        setContentLayout(R.layout.activity_mobile_phone_identification_step_one);
    }

    @Override
    public void initMember() {
        mStrMobile = UserXML.getMobilePhone(this);
        identificationTvMobile.setText(mStrMobile.replaceAll("(?<=[\\d]{3})\\d(?=[\\d]{4})", "*"));
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_SEND_VERIFY_CODE_TIP:
                        if (isFinishing())
                            return;
                        identificationBtAuthCode.setClickable(false);
                        identificationBtAuthCode.setTextColor(Color.parseColor("#b3b3b3"));
                        identificationBtAuthCode.setText(String.valueOf(msg.obj));
                        identificationBtAuthCode.setBackgroundResource(R.drawable.shape_bt_auth_code_disable);
                        break;
                    case MSG.MSG_SEND_VERIFY_CODE_END:
                        if (isFinishing())
                            return;
                        identificationBtAuthCode.setClickable(true);
                        identificationBtAuthCode.setTextColor(getResources().getColor(R.color.app_color));
                        identificationBtAuthCode.setText("发送验证码");
                        identificationBtAuthCode.setBackgroundResource(R.drawable.shape_rect_app_color);
                        break;
                    case MSG.MSG_SEND_PHONE_VERIFY_CODE_SUCCESS:
                        Utils.startForbitSendVerifyCode(MobilePhoneIdentificationStepOneActivity.this,
                                mHandler, TIME.SEND_CHECKCODE_TIME, null);
                        break;
                    case MSG.MSG_SEND_PHONE_VERIFY_CODE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_CHECK_PHONE_VERIFY_CODE_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_CHECK_PHONE_VERIFY_CODE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        GotoUtil.gotoActivity(this, MobilePhoneIdentificationStepTwoActivity.class);
    }

    @Override
    public void rightOnClick() {

    }

    @OnClick({R.id.identification_bt_auth_code, R.id.identification_bt_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.identification_bt_auth_code:
                getAuthCode();
                break;
            case R.id.identification_bt_next:
                next();
                break;
        }
    }

    /**
     * 获取输入内容 点击下一步
     */
    private void next() {

        mStrAuthCode = StringUtil.getEditText(identificationEtAuthCode);

        if (StringUtil.isStrEmpty(mStrMobile)) {
            showToast("手机号不能为空");
            return;
        }

        if (StringUtil.isStrEmpty(mStrAuthCode)) {
            showToast("请填写验证码");
            return;
        }

        BusinessAccount.checkPhoneVerifyCode(this, mStrMobile, mStrAuthCode, mHandler);
    }

    /**
     * 获取验证码
     */
    private void getAuthCode() {
        if (StringUtil.isStrEmpty(mStrMobile)) {
            showToast("手机号不能为空");
            return;
        }

        BusinessAccount.sendPhoneVerifyCode(this, "", mStrMobile, mHandler);

    }
}
